package a08_回溯算法;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * a02_组合复习4
 * </p>
 *
 * @author flyduck
 * @since 2025/5/20
 */
public class a02_组合复习4 {
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> result = new ArrayList<>();
        LinkedList<Integer> path = new LinkedList<>();
        traversal(result, path, 1, n, k);
        return result;
    }

    private void traversal(List<List<Integer>> result,
                           LinkedList<Integer> path,
                           int startNum,
                           int n,
                           int k
                           ){
        if(path.size() == k){
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = startNum; i <= n; i++) {
            path.add(i);
            traversal(result, path, i + 1, n, k);
            path.removeLast();
        }
    }
}
